<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/3
 * Time: 18:51
 */

namespace App\Http\Controllers\Admin;

use App\Model\PartnerRecord;
use App\Model\RecordList;
use App\Model\RecordrRecharge;
use App\Model\RecordTransferAccounts;
use App\Model\ShopOrder;
use Illuminate\Http\Request;
use App\Model\Members;
use App\Model\BankCard;
use App\Model\RecordTransaction;
use App\Model\RecordWithdraw;
use App\Model\Poundage;
use App\Model\RecordBonus;
use Session;
use Auth;
use Excel;

class FinanceController extends BaseController
{
    /**
     * 提现列表
     */
    public function withdrawlist()
    {
        return view('Admin.withdrawlist');
    }

    /**
     * 提现记录
     */
    public function withdraw(Request $request)
    {
        $data = $request->all();
        if (empty($data)) {
            return ['code' => 0, 'data' => '参数错误'];
        }
        //会员表
        $UserModel = new Members();
        //银行卡列表
        $BankCard = new BankCard();
        //交易记录表
        $RecordTransaction = new RecordTransaction();
        //提现记录表
        $RecordWithdraw = new RecordWithdraw();
        switch ($data['class']) {
            case 'select': //查询
                //关键字
                $keyword = empty($data['keyword']) ? 0 : $data['keyword'];
                //获取数据
                $rico = $this->getFinance($RecordWithdraw, $keyword);
//
                if ($rico['rico']) {
                    return ['code' => 1, 'data' => $rico['rico'],'total' => $rico['total'],'actual_money' => $rico['actual_money']];
                } else {
                    return ['code' => 0, 'data' => '没有数据'];
                }
                break;
            case 'revamp':    //修改
                if (empty($data['withdraw'])) {
                    return ['code' => 0, 'data' => '设置失败'];
                }
                $data['withdraw']['ip'] = $_SERVER["REMOTE_ADDR"];
                $data['withdraw']['audit_time'] = time();
                $data['withdraw']['operator'] = Auth::user()->name;
                if($data['withdrawal_type'] == 1){
                    $rico = $RecordWithdraw->where(['id' => $data['id']])->update($data['withdraw']);
                    if ($rico) {
                        if ($data['withdraw']['status'] == '-1') {
                            $reData = $RecordWithdraw->where('id', $data['id'])->first();
                            $user = Members::where('uid', $reData['uid'])->select('credit1')->first();
                            $userData['credit1'] = $user['credit1'] + $reData['money'];
                            $res = Members::where('uid', $reData['uid'])->update(array_only($userData, ['credit1']));
                            $deposit = [
                                'uid' => $reData['uid'],
                                'ordersn' => ORDER_SN(),
                                'money' => $reData['money'],
                                'type' => 3, //1 订单 2 转账 3 提现
                                'time' => time(),
                                'explain' => '积分提现驳回',
                                'updated_at' => date('Y-m-d H:i:s', time()),
                                'created_at' => date('Y-m-d H:i:s', time()),
                            ];
                            $orderId = $RecordTransaction->add_transaction($deposit);
                            if ($orderId) {
                                return ['code' => 1, 'data' => '设置成功'];
                            } else {
                                return ['code' => 0, 'data' => '设置失败'];
                            }
                        }
                        return ['code' => 1, 'data' => '设置成功'];
                    } else {

                        return ['code' => 0, 'data' => '设置失败'];
                    }
                }
                if($data['withdrawal_type'] == 2){
                    $rico = $RecordWithdraw->where(['id' => $data['id']])->update($data['withdraw']);
                    if ($rico) {
                        if ($data['withdraw']['status'] == '-1') {
                            $reData = $RecordWithdraw->where('id', $data['id'])->first();
                            $user = Members::where('uid', $reData['uid'])->select('credit2')->first();
                            $userData['credit2'] = $user['credit2'] + $reData['money'];
                             Members::where('uid', $reData['uid'])->update(array_only($userData, ['credit2']));
                            $deposit = [
                                'uid' => $reData['uid'],
                                'ordersn' => ORDER_SN(),
                                'money' => $reData['money'],
                                'type' => 3, //1 订单 2 转账 3 提现
                                'time' => time(),
                                'explain' => '奖励积分提现驳回',
                                'updated_at' => date('Y-m-d H:i:s', time()),
                                'created_at' => date('Y-m-d H:i:s', time()),
                            ];
                            $res = $RecordTransaction->add_transaction($deposit);
                            if ($res) {
                                return ['code' => 1, 'data' => '设置成功'];
                            } else {
                                return ['code' => 0, 'data' => '设置失败'];
                            }
                        }
                        return ['code' => 1, 'data' => '设置成功'];
                    } else {

                        return ['code' => 0, 'data' => '设置失败'];
                    }
                }
                if($data['withdrawal_type'] == 3){
                    $rico = $RecordWithdraw->where(['id' => $data['id']])->update($data['withdraw']);
                    if ($rico) {
                        if ($data['withdraw']['status'] == '-1') {
                            $reData = $RecordWithdraw->where('id', $data['id'])->first();
                            $user = Members::where('uid', $reData['uid'])->select('credit5')->first();
                            $userData['credit5'] = $user['credit5'] + $reData['money'];
                            Members::where('uid', $reData['uid'])->update(array_only($userData, ['credit5']));
                            $deposit = [
                                'uid' => $reData['uid'],
                                'ordersn' => ORDER_SN(),
                                'money' => $reData['money'],
                                'type' => 3, //1 订单 2 转账 3 提现
                                'time' => time(),
                                'explain' => '奖励积分提现驳回',
                                'updated_at' => date('Y-m-d H:i:s', time()),
                                'created_at' => date('Y-m-d H:i:s', time()),
                            ];
                            $res = $RecordTransaction->add_transaction($deposit);
                            if ($res) {
                                return ['code' => 1, 'data' => '设置成功'];
                            } else {
                                return ['code' => 0, 'data' => '设置失败'];
                            }
                        }
                        return ['code' => 1, 'data' => '设置成功'];
                    } else {

                        return ['code' => 0, 'data' => '设置失败'];
                    }
                }
                break;
            case 'find':   //查询一条数据
                $bank = $BankCard->where('id', $data['id'])->first();
                if ($bank) {
                    return ['code' => 1, 'data' => $bank];
                } else {
                    return ['code' => 0, 'data' => '设置失败'];
                }
                break;
            case 'delete': //删除
                if ($data['id']) {
                    $rico = $BankCard->where('id', $data['id'])->delete();
                    if ($rico) {
                        return ['code' => 1, 'data' => '删除成功'];
                    } else {
                        return ['code' => 0, 'data' => '删除失败'];
                    }
                } else {
                    return ['code' => 0, 'data' => '数据错误'];
                }
                break;
        }
    }

    /*
     * 交易记录
     */
    public function transaction()
    {
        return view('Admin.transaction');
    }

    /*
     * 充值记录
     */
    public function recharge()
    {
        return view('Admin.recharge');
    }

    /*
     * 转账记录
     */
    public function transfer()
    {
        return view('Admin.transfer');
    }

    /*
     * 奖励记录
     */
    public function rewards()
    {
        return view('Admin.rewards');
    }
    /*
         * 分红记录
         */
    public function bonus()
    {
        return view('Admin.bonus_list');
    }
    /*
     * 财务
     */
    public function doTransaction(Request $request)
    {
        //接收数据
        $data = $request->all();
        //当前用户
        $user = Auth::user();
        if (empty($data)) {
            return ['code' => 0, 'data' => '参数错误'];
        }
        switch ($data['class']) {
            //交易明细
            case 'select':
                //关键字
                $keyword = empty($data['keyword']) ? 0 : $data['keyword'];
                $RecordTransactionModel = new RecordTransaction();
                $transactionData = $this->getTransaction($RecordTransactionModel, $keyword);
                //是否是分公司
                /*
                   if ($user->is_branch != 0) {
                   $transactionData = $RecordTransactionModel->where('record_transaction.company_id', $user->is_branch)
                       ->join('members', 'record_transaction.uid', '=', 'members.uid')
                       ->select(
                           'members.user_name',
                           'members.phone',
                           'record_transaction.id',
                           'record_transaction.uid',
                           'record_transaction.ordersn',
                           'record_transaction.money',
                           'record_transaction.type',
                           'record_transaction.time',
                           'record_transaction.remark',
                           'record_transaction.explain')
                       ->orderBy('id', 'desc');
                   //订单类型
                   if (!empty($keyword['type'])) {
                       $transactionData = $transactionData->where('record_transaction.type', $keyword['type']);
                   }

                   //时间
                   if (!empty($keyword['time'])) {
                       $transactionData = $transactionData->where('time', '>', strtotime($keyword['time'][0]))->where('time', '<', strtotime($keyword['time'][1]));
                   }
                   //关键字
                   if (!empty($keyword['keyword'])) {
                       $transactionData = $transactionData->where('user_name', 'like', '%' . $keyword['keyword'] . '%')->orWhere('explain', 'like', '%' . $keyword['keyword'] . '%');
                   }
               } else {
                   $transactionData = $RecordTransactionModel->join('members', 'record_transaction.uid', '=', 'members.uid')
                       ->select(
                           'members.user_name',
                           'members.phone',
                           'record_transaction.id',
                           'record_transaction.uid',
                           'record_transaction.ordersn',
                           'record_transaction.money',
                           'record_transaction.type',
                           'record_transaction.time',
                           'record_transaction.remark',
                           'record_transaction.explain')
                       ->orderBy('id', 'desc');
                   //订单类型
                   if (!empty($keyword['type'])) {
                       $transactionData = $transactionData->where('record_transaction.type', $keyword['type']);
                   }

                   //时间
                   if (!empty($keyword['time'])) {
                       $transactionData = $transactionData->where('time', '>', strtotime($keyword['time'][0]))->where('time', '<', strtotime($keyword['time'][1]));
                   }
                   //订单
                   $orderSum = '0';
                   //转账
                   $transferSum = '0';
                   //转出
                   $rolloutSum = '0';
                   //转入
                   $shifttoSum = '0';
                   //提现
                   $withdrawSum = '0';
                   //充值
                   $rechargeSum = '0';
                   //奖励
                   $awardSum = '0';
                   //关键字
                   if (!empty($keyword['keyword'])) {
                       $transactionData = $transactionData->where('members.user_name', 'like', '%' . $keyword['keyword'] . '%')->orWhere('members.phone', 'like', '%' . $keyword['keyword'] . '%')->orWhere('ordersn', 'like', '%' . $keyword['keyword'] . '%')->orWhere('explain', 'like', '%' . $keyword['keyword'] . '%');
                       $numData = $transactionData->get();
                       foreach ($numData as $v) {
                           if ($v['type'] == 1) {
                               $orderSum += $v['money'];
                           }
                           if ($v['type'] == 2) {
                               if ($v['money'] >= 1) {
                                   //转入
                                   $shifttoSum += $v['money'];
                               } else {
                                   //转出
                                   $rolloutSum += $v['money'];
                               }
                           }
                           if ($v['type'] == 3) {
                               $withdrawSum += $v['money'];
                           }
                           if ($v['type'] == 4) {
                               $rechargeSum += $v['money'];
                           }
                           if ($v['type'] == 5) {
                               $awardSum += $v['money'];
                           }
                       }
                   } else {
                       $orderSum = $RecordTransactionModel->where(['record_transaction.type' => 1])->sum('money');
                       $shifttoSum = $RecordTransactionModel->where(['record_transaction.type' => 2])->where('record_transaction.money', '<', 0)->sum('money');
                       $rolloutSum = $RecordTransactionModel->where(['record_transaction.type' => 2])->where('record_transaction.money', '>', 0)->sum('money');
                       $withdrawSum = $RecordTransactionModel->where(['record_transaction.type' => 3])->sum('money');
                       $rechargeSum = $RecordTransactionModel->where(['record_transaction.type' => 4])->sum('money');
                       $awardSum = $RecordTransactionModel->where(['record_transaction.type' => 5])->sum('money');
                   }
               }
               $transactionData = $transactionData->paginate(15);

               foreach ($transactionData as $v) {
                   $v['time'] = date('Y-m-d H:i:s', $v['time']);
               }
*/
                if ($transactionData) {
                    return $transactionData;
                } else {
                    return ['code' => 0, 'data' => '没有数据'];
                }
                break;
            //充值记录
            case 'rechargeList':
                //关键字
                $keyword = empty($data['keyword']) ? 0 : $data['keyword'];
                //判断是否是公司
                if ($user->is_branch != 0) {
                    $rechargeData = RecordrRecharge::where('record_recharge.company_id', $user->is_branch)
                        ->join('members', 'record_recharge.uid', '=', 'members.uid')
                        ->join('record_transaction', 'record_recharge.tid', '=', 'record_transaction.id')
                        ->select(
                            'members.user_name',
                            'members.name',
                            'record_transaction.explain',
                            'record_transaction.money',
                            'record_recharge.uid',
                            'record_recharge.tid',
                            'record_recharge.type',
                            'record_recharge.credit',
                            'record_recharge.operator',
                            'record_recharge.createtime',
                            'record_recharge.ip')
                        ->orderBy('id', 'desc');;
                    //货币类型
                    if (!empty($keyword['type'])) {
                        $rechargeData = $rechargeData->where('type', $keyword['type']);
                    }
                    //时间
                    if (!empty($keyword['createtime'])) {
                        $rechargeData = $rechargeData->where('record_recharge.createtime', '>', strtotime($keyword['createtime'][0]))->where('record_recharge.createtime', '<', strtotime($keyword['createtime'][1]));
                    }
                    //关键字
                    if (!empty($keyword['keyword'])) {
                        $rechargeData = $rechargeData->where('user_name', 'like', '%' . $keyword['keyword'] . '%')->orWhere('name', 'like', '%' . $keyword['keyword'] . '%');
                    }
                } else {
                    $rechargeData = RecordrRecharge::join('members', 'record_recharge.uid', '=', 'members.uid')
//                        ->join('record_transaction', 'record_recharge.tid', '=', 'record_transaction.id')
                        ->select(
                            'members.user_name',
                            'members.name',
                            'record_recharge.uid',
                            'record_recharge.tid',
                            'record_recharge.type',
                            'record_recharge.credit',
                            'record_recharge.operator',
                            'record_recharge.createtime',
//                            'record_transaction.explain',
                            'record_recharge.money',
                            'record_recharge.ip')
                        ->orderBy('createtime', 'desc');
                    //货币类型
                    if (!empty($keyword['type'])) {
                        $rechargeData = $rechargeData->where('record_recharge.type', $keyword['type']);
                    }
                    //时间
                    if (!empty($keyword['dotime'])) {
                        $keyword['dotime'][0] = strtotime($keyword['dotime'][0]);
                        $keyword['dotime'][1] = strtotime($keyword['dotime'][1]);
                        $rechargeData = $rechargeData->where('createtime', '>', $keyword['dotime'][0])->where('createtime', '<', $keyword['dotime'][1]);
                    }
                    //关键字
                    if (!empty($keyword['keyword'])) {
                        $rechargeData = $rechargeData->where('user_name', 'like', '%' . $keyword['keyword'] . '%')->orWhere('name', 'like', '%' . $keyword['keyword'] . '%');
                    }
                }
                $total = $rechargeData->sum('record_recharge.money');
                $rechargeData = $rechargeData->paginate(15);
                foreach ($rechargeData as $v) {
                    $v['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
                }
                if ($rechargeData) {
                    return ['code' => 1, 'data' => $rechargeData,'total' => $total];
                } else {
                    return ['code' => 0, 'data' => '没有数据'];
                }
                break;
            // 兑换记录
            case "transferList":
                $keyword = empty($data['keyword']) ? 0 : $data['keyword'];
                //判断是否是公司
                if ($user->is_branch != 0) {
                    $rechargeData = RecordTransferAccounts::where('record_transfer_accounts.company_id', $user->is_branch)
                        ->join('members', 'record_transfer_accounts.uid', '=', 'members.uid')
                        ->join('record_transaction', 'record_transfer_accounts.tid', '=', 'record_transaction.id')
                        ->select(
                            'members.user_name',
                            'record_transfer_accounts.uid',
                            'record_transfer_accounts.tid',
                            'record_transfer_accounts.type',
                            'record_transfer_accounts.money',
                            'record_transfer_accounts.transfer',
                            'record_transfer_accounts.nickname',
                            'record_transfer_accounts.remark',
                            'record_transaction.ordersn',
                            'record_transfer_accounts.time')
                        ->paginate(15);
                } else {
                    $rechargeData = RecordTransferAccounts::join('members', 'record_transfer_accounts.uid', '=', 'members.uid')
                        ->join('record_transaction', 'record_transfer_accounts.tid', '=', 'record_transaction.id')
                        ->select(
                            'members.name',
                            'members.user_name',
                            'record_transfer_accounts.uid',
                            'record_transfer_accounts.tid',
                            'record_transfer_accounts.type',
                            'record_transfer_accounts.money',
                            'record_transfer_accounts.transfer',
                            'record_transfer_accounts.nickname',
                            'record_transfer_accounts.remark',
                            'record_transaction.ordersn',
                            'record_transfer_accounts.time')
                        ->orderBy('record_transfer_accounts.id', 'desc');

                    //货币类型
                    if (!empty($keyword['type'])) {
                        $rechargeData = $rechargeData->where('record_transfer_accounts.type', $keyword['type']);
                    }
                    //时间
                    if (!empty($keyword['time'])) {
                        $rechargeData = $rechargeData->where('time', '>', $keyword['time'][0])->where('time', '<', $keyword['time'][1]);
                    }
                    //关键字
                    if (!empty($keyword['keyword'])) {
                        $rechargeData = $rechargeData->where('members.user_name', 'like', '%' . $keyword['keyword'] . '%')->orWhere('name',$keyword['keyword']);
                    }

                }
//                $joinTotal = 0;
//                $shareTotal = 0;
//                $allRecord = $rechargeData->get()->toArray();
//                foreach ($allRecord as $v){
//                    if ($v['type'] == 1){
//                        $joinTotal += $v['money'];
//                    }
//                    if ($v['type'] == 2){
//                        $shareTotal += $v['money'];
//                    }
//                }
//                // paginate(15);
                $rechargeData = $rechargeData->paginate(15);
//                dump();
                foreach ($rechargeData as $v) {
                    $v['time'] = date('Y-m-d H:i:s', $v['time']);
                }
                if ($rechargeData) {
                    return ['code' => 1, 'data' => $rechargeData];
//                    ,'joinTotal' => $joinTotal,'shareTotal' => $shareTotal
                } else {
                    return ['code' => 0, 'data' => '没有数据'];
                }
                break;
            //奖励记录
            case 'rewardsList':
                $keyword = empty($data['keyword']) ? 0 : $data['keyword'];
//                //判断是否是分公司
//                if ($user->is_branch != 0) {
//                    $rewardsData = RecordBonus::where('record_bonus.company_id', $user->is_branch)
//                        ->join('members', 'record_bonus.uid', '=', 'members.uid')
//                        ->join('record_transaction', 'record_bonus.tid', '=', 'record_transaction.id')
//                        ->select(
//                            'members.user_name',
//                            'record_bonus.uid',
//                            'record_bonus.tid',
//                            'record_bonus.money',
//                            'record_bonus.remark',
//                            'record_bonus.time')
//                        ->orderBy('record_bonus.id', 'desc')
//                        ->paginate(15);
//                } else
//                    {
                    $rewardsData = RecordBonus::join('members', 'record_bonus.uid', '=', 'members.uid')
                        ->join('record_transaction', 'record_bonus.tid', '=', 'record_transaction.id')
                        ->select(
                            'members.user_name',
                            'members.name',
                            'record_bonus.uid',
                            'record_bonus.tid',
                            'record_bonus.money',
                            'record_bonus.remark',
                            'record_bonus.type',
                            'record_bonus.time')
                        ->orderBy('record_bonus.id', 'desc');
//                        ->paginate(15);
//                }
                //类型
                if (!empty($keyword['type'])) {
                    $rewardsData = $rewardsData->where('record_bonus.type',$keyword['type']);
                }
                //时间
                if (!empty($keyword['time'])) {
                    $keyword['time'][0] = strtotime($keyword['time'][0]);
                    $keyword['time'][1] = strtotime($keyword['time'][1]);
                    $rewardsData = $rewardsData ->where('record_bonus.time', '>', $keyword['time'][0])->where('record_bonus.time', '<', $keyword['time'][1]);
                }
                //关键字
                if (!empty($keyword['keyword'])) {
                    //var_dump($keyword['keyword']);
                    //exit;
                    $rewardsData = $rewardsData
                        ->where('tid', 'like', '%' . $keyword['keyword'] . '%')
                        ->orWhere('record_transaction.ordersn', 'like', '%' . $keyword['keyword'] . '%')
                        ->orWhere('user_name', $keyword['keyword'] )
                        ->orWhere('name',$keyword['keyword']);
                }
//                $allRecord = $rewardsData->get()->toArray();
//                //统计累计金额
//                $joinTotal = 0;
//                $shareTotal = 0;
//                foreach ($allRecord as $k =>$v){
//                    if ($v['type'] == 1){
//                        $joinTotal += $v['money'];
//                    }
//                    if ($v['type'] == 2){
//                        $shareTotal += $v['money'];
//                    }
//                }
                $rewardsData = $rewardsData->paginate(15);
                foreach ($rewardsData as $v) {
                    $v['time'] = date('Y-m-d H:i:s', $v['time']);
                }
                //var_dump($rewardsData);
                // exit;
                if ($rewardsData) {
                    return ['code' => 1, 'data' => $rewardsData];
//                    ,'joinTotal' => $joinTotal,'shareTotal' => $shareTotal
                } else {
                    return ['code' => 0, 'data' => '没有数据'];
                }
                break;
                //分红记录
            case 'bonusList':
                $keyword = empty($data['keyword']) ? 0 : $data['keyword'];
                $rewardsData = RecordList::orderBy('id', 'desc');
                //类型
                if (!empty($keyword['type'])) {
                    $rewardsData = $rewardsData->where('type',$keyword['type']);
                }
                //时间
                if (!empty($keyword['time'])) {
                    $keyword['time'][0] = strtotime($keyword['time'][0]);
                    $keyword['time'][1] = strtotime($keyword['time'][1]);
                    $rewardsData = $rewardsData ->where('rtime', '>', $keyword['time'][0])->where('time', '<', $keyword['time'][1]);
                }
                //关键字
                if (!empty($keyword['keyword'])) {
                    $rewardsData = $rewardsData
                        ->orWhere('bonus_sn', 'like', '%' . $keyword['keyword'] . '%');
                }
                $rewardsData = $rewardsData->paginate(15);
                if ($rewardsData) {
                    return ['code' => 1, 'data' => $rewardsData];
                } else {
                    return ['code' => 0, 'data' => '没有数据'];
                }
                break;
        }

    }

    //分红记录相关操作
    public function bonusList( Request $request )
    {
        $data = $request->all();
        $ListModel = new RecordList();
        $UserModel = new Members();
        switch ($data['class']){
            case 'find':
                $records = $ListModel->where('id',$data['id'])->first();
                if ($records){
                    return ['code' => 1,'data' => $records];
                } else {
                    return ['code' => 0,'data' => '记录不存在'];
                }
                break;
            //修改分红记录
            case 'edit':
                $record = $ListModel->where('id',$data['id'])->first();
                if ($record['status'] == 1){
                    return ['code' => 0,'data' => '该分红已发放,无法修改'];
                }
                $totalMoney = $data['money'];
                if ($record['type'] == 3){
                    $member = $UserModel->where('is_credit6','>','0')->where('platform_profit',1)->sum('is_credit6');
                    $integral = RETAIN_TWO( $data['money']/$member);
                    $num = $UserModel->where('is_credit6','>','0')->where('platform_profit',1)->count();
                    $members = $UserModel->where('is_credit6','>','0')->where('platform_profit',1)->get()->toArray();
                    foreach ($members as $k => $v){
                        $money2 = RETAIN_TWO($integral * $v['is_credit6']);
                        //记录订单之前各会员的奖励币余额记录
                        $UserModel->where('uid',$v['uid'])->update(['single_achievement' => $money2]);

                    }
                    if ($data['submit'] == 1){
                        // 系统操作员
                        $adminUser = Auth::user();
                        $newData = [
                            'total_amount' => $data['money'],
                            'user_num' => $num,
                            'number' => $member,
                            'single' => $integral,
                            'type' => 3,
                            'bonus_sn' => $data['bonus_sn'],
                            'verifier' => $adminUser['user_name'] ,
                            'status' => 0 ,
                            'updated_at' => date("Y-m-d H:i:s",time()) ,
                        ];
                        $res = $ListModel->where('id',$data['id'])->update($newData);
                        if ($res){
                            return ['code' => 1,'data' => '修改成功'];
                        } else {
                            return ['code' => 0,'data' => '修改失败'];
                        }
                    } else {
                        return ['code' => 1,'pre' => $integral,'count' => $num,'total_num'=>$member];
                    }
                }
                if ($record['type'] == 2){
                    $member = $UserModel->where('credit3','>','0')->where('platform_profit',1)->sum('credit3');
                    $integral = RETAIN_TWO( $data['money']/$member);
                    $num = $UserModel->where('credit3','>','0')->where('platform_profit',1)->count();
                    $members = $UserModel->where('credit3','>','0')->where('platform_profit',1)->get()->toArray();
                    foreach ($members as $k => $v){
                        $money2 = RETAIN_TWO($integral * $v['credit3']);
                        //记录订单之前各会员的奖励币余额记录
                        $UserModel->where('uid',$v['uid'])->update(['team_achievement' => $money2]);

                    }
                    if ($data['submit'] == 1){
                        // 系统操作员
                        $adminUser = Auth::user();
                        $newData = [
                            'total_amount' => $data['money'],
                            'user_num' => $num,
                            'number' => $member,
                            'single' => $integral,
                            'type' => 2,
                            'bonus_sn' => $data['bonus_sn'],
                            'verifier' => $adminUser['user_name'] ,
                            'status' => 0 ,
                            'updated_at' => date("Y-m-d H:i:s",time()) ,
                        ];
                        $res = $ListModel->where('id',$data['id'])->update($newData);
                        if ($res){
                            return ['code' => 1,'data' => '修改成功'];
                        } else {
                            return ['code' => 0,'data' => '修改失败'];
                        }
                    } else {
                        return ['code' => 1,'pre' => $integral,'count' => $num,'total_num'=>$member];
                    }
                }
                if ($record['type'] == 1){
                    if ($data['money'] < 0){
                        return ['code' => 0,'data' => '分红金额有误！请重新输入！'];
                    }
                    $member = $UserModel->where('voucher','>','0')->where('platform_profit',1)->sum('voucher');
                    $integral = RETAIN_TWO( $data['money']/$member);
                    $num = $UserModel->where('voucher','>','0')->where('platform_profit',1)->count();
                    $members = $UserModel->where('voucher','>','0')->where('platform_profit',1)->get()->toArray();
//                    foreach ($members as $k => $v){
////                        $micro = ShopOrder::where(['uid' => $v['uid'],'ordertype' => 2])->where('status','>=',1)->value('price');
//                        if ($v['consumption_bonus'] > $v['credit3'] ){
//                            //分红上限
//                            $results =  $UserModel->where('uid',$v['uid'])->update(['is_out' => 0,'voucher' => 0,'consumption_bonus' => 0]);
//                            $RecordTransaction = new RecordTransaction();
//                            if ($results){
//                                $recommendData = [
//                                    'uid' => $v['uid'],
//                                    'ordersn' => $data['bonus_sn'],
//                                    'money' => 0,
//                                    'type' => 1,
//                                    'time' => time(),
//                                    'explain' => '已出局',
//                                    'remark' => '已出局' ,
//                                ];
//                                $RecordTransaction->add_transaction($recommendData);
//                            } else {
//                                return ['code' => 0,'data' => '分红失败'];
//                            }
//                            unset($members[$k]);
//                        }
//
//                    }
                    if ($data['submit'] == 1){
                        // 系统操作员
                        $adminUser = Auth::user();
                        $newData = [
                            'total_amount' => $data['money'],
                            'remark' => $data['remark'],
                            'user_num' => $num,
                            'number' => $member,
                            'single' => $integral,
                            'type' => 1,
                            'bonus_sn' => $data['bonus_sn'],
                            'verifier' => $adminUser['user_name'] ,
                            'status' => 0 ,
                            'updated_at' => date("Y-m-d H:i:s",time()) ,
                        ];
                        $res = $ListModel->where('id',$data['id'])->update($newData);
                        if ($res){
                            return ['code' => 1,'data' => '修改成功'];
                        } else {
                            return ['code' => 0,'data' => '修改失败'];
                        }
                    } else {
                        return ['code' => 1,'pre' => $integral,'count' => $num,'total_num'=>$member];
                    }
                }
                break;
            //确认发放分红
            case 'submit':
                $PoundageModel = new Poundage();
                $Poundage = $PoundageModel->first();
                $record = $ListModel->where('id',$data['id'])->first();
                if($record['status'] == 1){
                    return ['code' => 0,'data' => '此单已成功分红'];
                }
                //个人业绩发放
                if ($record['type'] == 1){
                    $members = $UserModel->where('voucher','>','0')->where('platform_profit',1)->get()->toArray();
                    foreach ($members as $k => $v){
//                        $micro = ShopOrder::where(['uid' => $v['uid'],'ordertype' => 2])->where('status','>=',1)->value('price');
//                        if ($v['consumption_bonus'] > $v['credit3']){
//                            //分红上限
//                            $results =  $UserModel->where('uid',$v['uid'])->update(['is_out' => 0,'voucher' => 0,'consumption_bonus' => 0]);
//                            $RecordTransaction = new RecordTransaction();
//                            if ($results){
//                                $recommendData = [
//                                    'uid' => $v['uid'],
//                                    'ordersn' => $record['bonus_sn'],
//                                    'money' => 0,
//                                    'type' => 1,
//                                    'time' => time(),
//                                    'explain' => '已出局',
//                                    'remark' => '已出局' ,
//                                ];
//                                $RecordTransaction->add_transaction($recommendData);
//                            } else {
//                                return ['code' => 0,'data' => '分红失败'];
//                            }
//                            unset($members[$k]);
//                        }
                        $money2 = RETAIN_TWO($record['single'] * $v['voucher']);
                        $recommendMoney = CALCULATD($Poundage, $money2, 2);
//                        $credit3 = $v['credit3'] - $money2;
                        //添加记录
                        $v['credit2'] += $recommendMoney['money'];
                        $v['credit5'] += $recommendMoney['redelivery'];
                        $v['consumption_bonus'] += $money2;
                        $v['achievement_increment'] = 0;

                        //记录订单之前各会员的奖励币余额记录
                        $res = $UserModel->where('uid',$v['uid'])->update(['achievement_increment' => 0,'credit2' => $v['credit2'],'credit5' => $v['credit5'],'consumption_bonus' => $v['consumption_bonus']]);
                        //重新赋值
                        $addData3['money'] = $money2;
                        $addData3['text'] = '贡献值加权补贴';
                        $addData3['remark'] = $record['remark'];
                        $addData3['recommendMoney'] = $recommendMoney;
                        $addData3['ordersn'] =  $record['bonus_sn'];
                        if ($res){
                            $this->addOrder($v['uid'],  $addData3['money'], 5, $addData3['text'], $addData3['recommendMoney'], 0, $addData3['ordersn'],$addData3['remark']);
                        }
                    }
                    //修改记录状态
                    $res = [
                        'status' => 1,
                        'updated_at'=>date('Y-m-d H:i', time())
                    ];
                    $rico = $ListModel->where('id',$data['id'])->update($res);
                    if ($rico){
                        return ['code' => 1,'data' => '分红成功'];
                    } else {
                        return ['code' => 0,'data' => '分红失败'];
                    }
                }
                //团队业绩发放
                if ($record['type'] == 2){
                    $members = $UserModel->where('credit3','>','0')->where('platform_profit',1)->get()->toArray();
                    foreach ($members as $k => $v){
                        $money2 = RETAIN_TWO($record['single'] * $v['credit3']);
                        $recommendMoney = CALCULATE($Poundage, $money2, 2);
                        //添加记录
                        $v['credit2'] += $recommendMoney['money'];
                        $v['credit5'] += $recommendMoney['redelivery'];
                        $v['team_bonus'] += $money2;
                        $v['team_achievement'] = 0;
                        //记录订单之前各会员的奖励币余额记录
                        $res = $UserModel->where('uid',$v['uid'])->update(['team_achievement' => 0,'credit2' => $v['credit2'],'credit5' => $v['credit5'],'team_bonus' => $v['team_bonus']]);
                        //重新赋值
                        $addData3['money'] = $money2;
                        $addData3['type'] = 5;
                        $addData3['text'] = $record['remark'];
                        $addData3['remark'] = 2;
                        $addData3['recommendMoney'] = $recommendMoney;
                        $addData3['ordersn'] =  $record['bonus_sn'];
                        if ($res){
                            $this->addOrder($v['uid'],  $addData3['money'], 5, $addData3['text'], $addData3['recommendMoney'], 0, $addData3['ordersn'],$addData3['remark']);

                        }
                    }
                    //修改记录状态
                    $rico = $ListModel->where('id',$data['id'])->update(['status' => 1,'time'=>date("Y-m-d H:i:s",time())]);
                    if ($rico){
                        return ['code' => 1,'data' => '分红成功'];
                    } else {
                        return ['code' => 0,'data' => '分红失败'];
                    }
                }
                //团队业绩发放
                if ($record['type'] == 3){
                    $members = $UserModel->where('is_credit6','>','0')->where('platform_profit',1)->get()->toArray();
                    foreach ($members as $k => $v){
                        $money2 = RETAIN_TWO($record['single'] * $v['is_credit6']);
                        $recommendMoney = CALCULATE($Poundage, $money2, 2);
                        //添加记录
                        $v['credit2'] += $recommendMoney['money'];
                        $v['credit5'] += $recommendMoney['redelivery'];
                        $v['team_bonus'] += $money2;
                        $v['single_achievement'] = 0;
                        //记录订单之前各会员的奖励币余额记录
                        $res = $UserModel->where('uid',$v['uid'])->update(['single_achievement' => 0,'credit2' => $v['credit2'],'credit5' => $v['credit5'],'team_bonus' => $v['team_bonus']]);
                        //重新赋值
                        $addData3['money'] = $money2;
                        $addData3['type'] = 5;
                        $addData3['text'] = $record['remark'];
                        $addData3['remark'] = 2;
                        $addData3['recommendMoney'] = $recommendMoney;
                        $addData3['ordersn'] =  $record['bonus_sn'];
                        if ($res){
                            $this->addOrder($v['uid'],  $addData3['money'], 5, $addData3['text'], $addData3['recommendMoney'], 0, $addData3['ordersn'],$addData3['remark']);
                        }
                    }
                    //修改记录状态
                    $rico = $ListModel->where('id',$data['id'])->update(['status' => 1,'time'=>date("Y-m-d H:i:s",time())]);
                    if ($rico){
                        return ['code' => 1,'data' => '分红成功'];
                    } else {
                        return ['code' => 0,'data' => '分红失败'];
                    }
                }

                break;

        }
    }
    /**
     * 添加交易记录
     * @param null $uid 会员id
     * @param null $money 交易总金额
     * @param null $type 交易类型 1 订单 2 转账 3 提现  4充值 5 奖励制度
     * @param string $explain 交易名称
     * @param array $manageMoney 奖励金额 复投金额 平台费金额
     * @param null $company_id 公司id
     */
    public function addOrder($uid = null, $money = null, $type = null, $explain = '', $manageMoney = array(), $company_id = null, $ordersn = '',$remark)
    {
        $ordersn = empty($ordersn) ? ORDER_SN() : $ordersn;
        //交易记录表
        $order_uid = ShopOrder::where('ordersn', $ordersn)->value('uid');
        $userName = Members::where('uid', $order_uid)->value('user_name');
        $RecordTransaction = new RecordTransaction();
        //交易记录表详情表
        $RecordBonus = new RecordBonus();
        $levelinDatas = [ //交易记录
            'uid' => $uid,
            'ordersn' => $ordersn,
            'money' => $money,
            'type' => $type,
            'time' => time(),
            'explain' => $explain,
            'remark' => $remark,
            'company_id' => $company_id,
        ];
        $TransactionId = $RecordTransaction->add_transaction($levelinDatas);
        $bonusData = [//交易详情
            'uid' => $uid,
            'tid' => $TransactionId,
            'money' => $money,
            'remark' => $explain . '：' . $manageMoney['money'] . '，复销币：' . $manageMoney['redelivery'] . '，平台费：' . $manageMoney['platform_fee'],
//            'remark' => $explain . '：' . $money. '，参与积分来源：用户' . $userName . '参与抢购' . ')',
            'time' => time(),
            'type' => 1,
            'company_id' => $company_id,
        ];
        $RecordBonus->add_bonus($bonusData);
    }
    /*
     * 导出明细
     */
    public function exportDetail(Request $request)
    {
//        ini_set(max_execution_time, 84000);
//        ini_set('max_execution_time', 84000);
        $info['keyword'] = $request->all();
        //检测是否有值
        $info['keyword'] = empty($info['keyword']) ? "" : $info['keyword'];
        //导出交易记录
        if ($info['keyword']['ClickType'] == 1) {
            $newData[] = ['编号', '订单编号', '用户', '电话', '金额', '交易时间', '交易类型', '备注'];
            $RecordTransaction = new RecordTransaction();
            //调用交易记录数据
            $transactionData = $this->getTransaction($RecordTransaction, $info['keyword'], true);
            /*老数据
            * $transactionData = RecordTransaction::join('members', 'record_transaction.uid', '=', 'members.uid')
                    ->select(
                        'members.user_name',
                        'members.phone',
                        'record_transaction.id',
                        'record_transaction.uid',
                        'record_transaction.ordersn',
                        'record_transaction.money',
                        'record_transaction.type',
                        'record_transaction.time',
                        'record_transaction.explain',
                        'record_transaction.remark'
                    )
                    ->orderBy('id', 'desc');
                //订单类型
                if (!empty($info['keyword']['type'])) {
                    $transactionData = $transactionData->where('record_transaction.type', $info['keyword']['type']);
                }
                //时间
                if (!empty($info['keyword']['time'])) {
                    $transactionData = $transactionData->where('time', '>', strtotime($info['keyword']['time'][0]))->where('time', '<', strtotime($info['keyword']['time'][1]));
                }
                //关键字
                if (!empty($info['keyword']['keyword'])) {
                    $transactionData = $transactionData->where('user_name', 'like', '%' . $info['keyword']['keyword'] . '%')->orWhere('phone', 'like', '%' . $info['keyword']['keyword'] . '%')->orWhere('explain', 'like', '%' . $info['keyword']['keyword'] . '%');

                }
                $transactionData = $transactionData->get()->toArray();
    */
            foreach ($transactionData['data'] as $v) {
                if ($v['type'] == 1) {
                    $v['type'] = '订单';
                } else if ($v['type'] == 2) {
                    $v['type'] = '转账';
                } else if ($v['type'] == 3) {
                    $v['type'] = '提现';
                } else if ($v['type'] == 4) {
                    $v['type'] = '充值';
                } else if ($v['type'] == 5) {
                    $v['type'] = '奖励制度';
                }else if ($v['type'] == 7) {
                    $v['type'] = '未中奖退款';
                }else if ($v['type'] == 8) {
                    $v['type'] = '未中奖积分红包';
                }
                $newData[] = [
                    $v['id'],
                    $v['ordersn'],
                    $v['user_name'],
                    $v['phone'],
                    $v['money'],
//                    $v['type'],
                    $v['time'],
                    $v['explain'],
                    $v['remark']
                ];
            }
            Excel::create("交易记录", function ($excel) use ($newData) {
                $excel->sheet('score', function ($sheet) use ($newData) {
                    $sheet->rows($newData);
                });
            })->export('xls');

            //充值记录
        } elseif ($info['keyword']['ClickType'] == 2) {
            $newData[] = ['编号', '姓名', '账号', '类型', '金额', '备注', '操作人', '时间', 'IP'];
            $rechargeData = RecordrRecharge::join('members', 'record_recharge.uid', '=', 'members.uid')
                ->join('record_transaction', 'record_recharge.tid', '=', 'record_transaction.id')
                ->select(
                    'members.user_name',
                    'members.name',
                    'record_recharge.id',
                    'record_recharge.uid',
                    'record_recharge.tid',
                    'record_recharge.type',
                    'record_recharge.credit',
                    'record_recharge.operator',
                    'record_recharge.createtime',
                    'record_transaction.explain',
                    'record_transaction.money',
                    'record_recharge.ip')
                ->orderBy('createtime');
            //货币类型
            if (!empty($info['keyword']['type'])) {
                $rechargeData = $rechargeData->where('record_recharge.type', $info['keyword']['type']);
            }
            //时间
            if (!empty($info['keyword']['dotime'])) {
                $dotime = explode(",", $info['keyword']['dotime']);
                $rechargeData = $rechargeData->where('createtime', '>', strtotime($dotime[0]))->where('createtime', '<', strtotime($dotime[1]));
            }
            //关键字
            if (!empty($info['keyword']['keyword'])) {
                $rechargeData = $rechargeData->where('user_name', 'like', '%' . $info['keyword']['keyword'] . '%')->orWhere('name', 'like', '%' . $info['keyword']['keyword'] . '%');
            }
            $rechargeData = $rechargeData->get();
            foreach ($rechargeData as $v) {
                $v['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
                if ($v['type'] == 1) {
                    $v['type'] = '积分';
                } else if ($v['type'] == 2) {
                    $v['type'] = '奖励积分';
                }
                $newData[] = [
                    $v['id'],
                    $v['user_name'],
                    $v['name'],
                    $v['type'],
                    $v['money'],
                    $v['explain'],
                    $v['operator'],
                    $v['createtime'],
                    $v['ip'],
                ];
            }
            Excel::create("充值记录", function ($excel) use ($newData) {
                $excel->sheet('score', function ($sheet) use ($newData) {
                    $sheet->rows($newData);
                });
            })->export('xls');

            //提现记录
        } else if ($info['keyword']['ClickType'] == 3) {
            $newData[] = ['编号', '姓名','账号','提现金额', '收款名称','收款账号','开户名','开户行','实际金额', '手续费', '申请日期', '审核日期', '审核人', '备注'];
            $RecordWithdraw = new RecordWithdraw();
            $rico = $RecordWithdraw->from('record_withdraw as a')
                ->join('members as b', 'a.uid', '=', 'b.uid')
                ->join('bank_card as c', 'a.bank_id', '=', 'c.id')
                ->select('b.user_name', 'b.phone','b.name', 'a.*', 'c.bank_name', 'c.card_number', 'c.name', 'c.bank_address')
                ->orderBy('a.id');
            //状态
            if (isset($info['keyword']['status'])) {
                $rico = $rico->where('status', $info['keyword']['status']);
            }
            //时间
            if (!empty($info['keyword']['withdraw_time'])) {
                $dotime = explode(",", $info['keyword']['withdraw_time']);
                $rico = $rico->where('audit_time', '>', strtotime($dotime[0]))->where('audit_time', '<', strtotime($dotime[1]));
            }
            //关键字
            if (!empty($info['keyword']['keyword'])) {
                $rico = $rico->where('user_name', 'like', '%' . $info['keyword']['keyword'] . '%')->orWhere('b.phone', 'like', '%' . $info['keyword']['keyword'] . '%');
            }
            //调用数据
            $rico = $this->getFinance($RecordWithdraw, $info['keyword'], false);
            foreach ($rico['rico'] as $v) {
                $v['withdraw_time'] = date('Y-m-d H:i:s', $v['withdraw_time']);
                $v['audit_time'] = date('Y-m-d H:i:s', $v['audit_time']);
                $newData[] = [
                    $v['id'],
                    $v['user_name'],
                    $v['name'],
                    $v['money'],
                    $v['beneficiary_name'],
                    "\t".$v['collection_number']."\t",
                    $v['account_name'],
                    $v['ip'],
                    $v['actual_money'],
                    $v['procedure'],
                    $v['withdraw_time'],
                    $v['audit_time'],
                    $v['operator'],
                    $v['remarks'],
                ];
            }
            Excel::create("提现记录" . date('Y-m-d'), function ($excel) use ($newData) {
                $excel->sheet('score', function ($sheet) use ($newData) {
                    $sheet->rows($newData);
                });
            })->export('xlsx');

            //兑换记录
        } else if ($info['keyword']['ClickType'] == 4) {
            $newData[] = ['单号','编号', '用户姓名', '交易类型', '总额', '交易方姓名', '备注', '时间'];
            $rechargeData = RecordTransferAccounts::join('members', 'record_transfer_accounts.uid', '=', 'members.uid')
                ->join('record_transaction', 'record_transfer_accounts.tid', '=', 'record_transaction.id')
                ->select(
                    'members.user_name',
                    'record_transfer_accounts.id',
                    'record_transfer_accounts.uid',
                    'record_transfer_accounts.tid',
                    'record_transfer_accounts.type',
                    'record_transfer_accounts.money',
                    'record_transfer_accounts.transfer',
                    'record_transfer_accounts.nickname',
                    'record_transfer_accounts.remark',
                    'record_transaction.ordersn',
                    'record_transfer_accounts.time')
                ->orderBy('record_transfer_accounts.id');

            //货币类型
            if (!empty($info['keyword']['type'])) {
                $rechargeData = $rechargeData->where('record_transfer_accounts.type', $info['keyword']['type']);
            }
            //时间
            if (!empty($info['keyword']['time'])) {
                $rechargeData = $rechargeData->where('time', '>', $info['keyword']['time'][0])->where('time', '<', $info['keyword']['time'][1]);
            }
            //关键字
            if (!empty($info['keyword']['keyword'])) {
                $rechargeData = $rechargeData->where('user_name', 'like', '%' . $info['keyword']['keyword'] . '%');
            }
        $rechargeData = $rechargeData->get()->toArray();
        foreach ($rechargeData as $v) {
            $v['time'] = date('Y-m-d H:i:s', $v['time']);
            if ($v['type'] == 1) {
                $v['type'] = '参与积分';
            } else if ($v['type'] == 2) {
                $v['type'] = '分享积分';
            }
            $newData[] = [
                $v['ordersn'],
                $v['id'],
                $v['user_name'],
                $v['type'],
                $v['money'],
                $v['nickname'],
                $v['remark'],
                $v['time'],
            ];
        }
        Excel::create("兑换记录", function ($excel) use ($newData) {
            $excel->sheet('score', function ($sheet) use ($newData) {
                $sheet->rows($newData);
            });
        })->export('xls');

        //奖励记录
        }else if ($info['keyword']['ClickType'] == 5){
            $newData[] = ['用户编号', '用户', '交易总额', '交易详情', '时间'];
            $user = Auth::user();
//            if ($user->is_branch != 0) {
//                $rewardsData = RecordBonus::where('record_bonus.company_id', $user->is_branch)
//                    ->join('members', 'record_bonus.uid', '=', 'members.uid')
//                    ->join('record_transaction', 'record_bonus.tid', '=', 'record_transaction.id')
//                    ->select(
//                        'members.user_name',
//                        'record_bonus.uid',
//                        'record_bonus.tid',
//                        'record_bonus.money',
//                        'record_bonus.remark',
//                        'record_bonus.time')
//                    ->orderBy('record_bonus.id', 'desc')
//                    ->get()
//                    ->toArray();
//            } else
//                {
                $rewardsData = RecordBonus::join('members', 'record_bonus.uid', '=', 'members.uid')
                    ->join('record_transaction', 'record_bonus.tid', '=', 'record_transaction.id')
                    ->select(
                        'members.user_name',
                        'record_bonus.uid',
                        'record_bonus.tid',
                        'record_bonus.money',
                        'record_bonus.remark',
                        'record_bonus.time')
                    ->orderBy('record_bonus.id', 'desc');
//            }

            if (!empty($info['keyword']['type'])) {
                $rewardsData = $rewardsData->where('record_bonus.type',$info['keyword']['type']);
            }
            //时间
            if (!empty($info['keyword']['time'])) {
                $info['keyword']['time'] = explode(',',$info['keyword']['time']);
                $keyword['time'][0] = strtotime($info['keyword']['time'][0]);
                $keyword['time'][1] = strtotime($info['keyword']['time'][1]);
                $rewardsData = $rewardsData ->where('record_bonus.time', '>', $keyword['time'][0])->where('record_bonus.time', '<', $keyword['time'][1]);
            }

            //关键字
            if (!empty($info['keyword']['keyword'])) {
                //var_dump($keyword['keyword']);
                //exit;
                $rewardsData = $rewardsData
                    ->where('tid', 'like', '%' . $info['keyword']['keyword'] . '%')
                    ->orWhere('record_transaction.ordersn', 'like', '%' . $info['keyword']['keyword'] . '%')
                    ->orWhere('record_bonus.remark', 'like', '%' . $info['keyword']['keyword'] . '%')
                    ->orWhere('user_name', 'like', '%' . $info['keyword']['keyword'] . '%');
            }
            $rewardsData = $rewardsData->get()->toArray();
            foreach ($rewardsData as $v) {
                $v['time'] = date('Y-m-d H:i:s', $v['time']);
                $newData[] = [
                    $v['uid'],
                    $v['user_name'],
                    $v['money'],
                    $v['remark'],
                    $v['time'],
                ];
            }
            Excel::create("奖励记录", function ($excel) use ($newData) {
                $excel->sheet('score', function ($sheet) use ($newData) {
                    $sheet->rows($newData);
                });
            })->export('xls');
        }else if($info['keyword']['ClickType'] == 7){
            $newData[] = ['分红编号', '分红总金额', '分红总人数', '总份额', '单份份额','分红类型','分红状态','操作员','分红时间'];
            $rewardsData = RecordList::orderBy('id', 'desc');
            if (!empty($info['keyword']['type'])) {
                $rewardsData = $rewardsData->where('type',$info['keyword']['type']);
            }
            //时间
            if (!empty($info['keyword']['time'])) {
                $info['keyword']['time'] = explode(',',$info['keyword']['time']);
                $keyword['time'][0] = strtotime($info['keyword']['time'][0]);
                $keyword['time'][1] = strtotime($info['keyword']['time'][1]);
                $rewardsData = $rewardsData ->where('time', '>', $keyword['time'][0])->where('time', '<', $keyword['time'][1]);
            }

            //关键字
            if (!empty($info['keyword']['keyword'])) {
                $rewardsData = $rewardsData
                    ->Where('bonus_sn', 'like', '%' . $info['keyword']['keyword'] . '%')
                    ->orWhere('remark', 'like', '%' . $info['keyword']['keyword'] . '%')
                    ->orWhere('verifier', 'like', '%' . $info['keyword']['keyword'] . '%');
            }
            $rewardsData = $rewardsData->get()->toArray();
            foreach ($rewardsData as $v) {
                if($v['type'] == 1){
                    $v['type'] = '消费分红';
                }else if($v['type'] == 2){
                    $v['type'] = '团队业绩分红';
                }
                if($v['status'] == 0){
                    $v['status'] = '未分红';
                }else if($v['status'] == 1){
                    $v['status'] = '已分红';
                }
//                $v['time'] = date('Y-m-d H:i:s', $v['time']);
                $newData[] = [
                    $v['bonus_sn'],
                    $v['total_amount'],
                    $v['user_num'],
                    $v['number'],
                    $v['single'],
                    $v['type'],
                    $v['status'],
                    $v['verifier'],
                    $v['time'],
                ];
            }
            Excel::create("分红记录", function ($excel) use ($newData) {
                $excel->sheet('score', function ($sheet) use ($newData) {
                    $sheet->rows($newData);
                });
            })->export('xls');
        }
    }

    /**
     * 提现数据查询
     * @param $RecordWithdraw
     * @param $keyword
     * @param $array
     * @return mixed
     */
    public function getFinance($RecordWithdraw, $keyword, $array = true)
    {
        //获取数据
        $rico = $RecordWithdraw->from('record_withdraw as a')
            ->join('members as b', 'a.uid', '=', 'b.uid')
//            ->join('bank_card as c', 'a.bank_id', '=', 'c.id')
            ->select('b.user_name', 'b.name','b.phone', 'a.*');
        //状态
        if (isset($keyword['status'])) {
            $rico = $rico->where('status', $keyword['status']);
        }
        //时间
        if (!empty($keyword['withdraw_time'])) {
            if ($array) {
                $rico = $rico->where('audit_time', '>', strtotime($keyword['withdraw_time'][0]))->where('audit_time', '<', strtotime($keyword['withdraw_time'][1]));
            } else {
                $rico = $rico->where('audit_time', '>', strtotime(explode(',', $keyword['withdraw_time'])[0]))->where('audit_time', '<', strtotime(explode(',', $keyword['withdraw_time'])[1]));
            }
        }
        //关键字查询(模糊查询)
        if (!empty($keyword['keyword'])) {
            $rico = $rico->where(function ($rico) use ($keyword) {
                $rico->where('b.phone', 'like', '%' . $keyword['keyword'] . '%')->orWhere('b.user_name', 'like', '%' . $keyword['keyword'] . '%');
            });
        }
        $total = 0;
        $actual_money = 0;
        $allRecord = $rico->get()->toArray();
        foreach ($allRecord as $v){
            $total += $v['money'];
            $actual_money += $v['actual_money'];
        }
        if ($array) {
            //显示
            $rico = $rico->orderBy('id', 'desc')->paginate(15);
        } else {
            //统计总金额
            $total = $rico->sum('money');
            //实际到账金额
            $actual_money = $rico->sum('actual_money');
            //下载
            $rico = $rico->orderBy('id', 'desc')->get();
        }
        if ($rico) {
            return ['rico' => $rico,'total' => $total,'actual_money' => $actual_money];
        } else {
            return false;
        }
    }

    /**
     * 交易明细数据
     * @param $RecordTransactionModel
     * @param $keyword
     * @param bool $array
     * @return array|bool
     */
    public function getTransaction($RecordTransactionModel, $keyword, $array = false)
    {
        $user = Auth::user();
        if ($user->is_branch != 0) {
            $transactionData = $RecordTransactionModel->where('record_transaction.company_id', $user->is_branch)
                ->join('members', 'record_transaction.uid', '=', 'members.uid')
                ->select(
                    'members.user_name',
                    'members.phone',
                    'record_transaction.id',
                    'record_transaction.uid',
                    'record_transaction.ordersn',
                    'record_transaction.money',
                    'record_transaction.type',
                    'record_transaction.time',
                    'record_transaction.remark',
                    'record_transaction.explain')
                ->orderBy('id', 'desc');
            //订单类型
            if (!empty($keyword['type'])) {
                $transactionData = $transactionData->where('record_transaction.type', $keyword['type']);
            }

            //时间查找
            if (!empty($keyword['time'] && $keyword['time'] != "null")) {
                if ($array) {
                    $transactionData = $transactionData->where('time', '>', strtotime(explode(',', $keyword['time'])[0]))->where('time', '<', strtotime(explode(',', $keyword['time'])[1]));
                } else {
                    $transactionData = $transactionData->where('time', '>', strtotime($keyword['time'][0]))->where('time', '<', strtotime($keyword['time'][1]));
                }
            }
            //关键字
            if (!empty($keyword['keyword'])) {
                $transactionData = $transactionData->where('user_name', 'like', '%' . $keyword['keyword'] . '%')->orWhere('explain', 'like', '%' . $keyword['keyword'] . '%');
            }
        } else {
            $transactionData = $RecordTransactionModel->join('members', 'record_transaction.uid', '=', 'members.uid')
                ->select(
                    'members.user_name',
                    'members.name',
                    'members.phone',
                    'record_transaction.id',
                    'record_transaction.uid',
                    'record_transaction.ordersn',
                    'record_transaction.money',
                    'record_transaction.type',
                    'record_transaction.time',
                    'record_transaction.remark',
                    'record_transaction.explain')
                ->orderBy('id', 'desc');
            //订单类型
            if (!empty($keyword['type'])) {
                $transactionData = $transactionData->where('record_transaction.type', $keyword['type']);
            }

            //时间
            if (!empty($keyword['time'] && $keyword['time'] != "null")) {
                if ($array) {
                    $transactionData = $transactionData->where('time', '>', strtotime(explode(',', $keyword['time'])[0]))->where('time', '<', strtotime(explode(',', $keyword['time'])[1]));
                } else {
                    $transactionData = $transactionData->where('time', '>', strtotime($keyword['time'][0]))->where('time', '<', strtotime($keyword['time'][1]));
                }
            }
            //关键字
            if (!empty($keyword['keyword'])) {
                $transactionData = $transactionData->where(function ($transactionData) use ($keyword) {
                    $transactionData->where('members.user_name', 'like', '%' . $keyword['keyword'] . '%')->orWhere('members.name', 'like', '%' . $keyword['keyword'] . '%')->orWhere('ordersn', 'like', '%' . $keyword['keyword'] . '%');
                });
            }
        }
//        //订单
//        $orderSum = '0';
//        //提现
//        $withdrawSum = '0';
//        //充值
//        $rechargeSum = '0';
//        //分享积分
//        $shareSum = '0';
//        //参与积分
//        $joinSum = '0';
//        //未中奖退款
//        $returnSum = '0';
//        //活动额外奖励
//        $activeSum = '0';
//        $allRecord = $transactionData->get()->toArray();
//        foreach ($allRecord as $k => $v){
//            if ($v['type'] == 1) {
//                $orderSum += $v['money'];
//            }
//            if ($v['type'] == 3) {
//                $withdrawSum += $v['money'];
//            }
//            if ($v['type'] == 4) {
//                $rechargeSum += $v['money'];
//            }
//            if ($v['type'] == 5) {
//                $shareSum += $v['money'];
//            }
//            if ($v['type'] == 8) {
//                $joinSum += $v['money'];
//            }
//            if ($v['type'] == 7){
//                $returnSum += $v['money'];
//            }
//            if ($v['type'] == 9){
//                $activeSum += $v['money'];
//            }
//        }
        if ($array) {
            //下载
            $transactionData = $transactionData->get();
            foreach ($transactionData as $v) {
                $v['time'] = date('Y-m-d H:i:s', $v['time']);
            }
        } else {
            //查看
            $transactionData = $transactionData->paginate(15);
            foreach ($transactionData as $v) {
                $v['time'] = date('Y-m-d H:i:s', $v['time']);
            }
        }
        if ($transactionData) {
            return ['code' => 1, 'data' => $transactionData ];
//            ,'orderSum' => $orderSum, 'withdrawSum' => $withdrawSum, 'rechargeSum' => $rechargeSum, 'shareSum' => $shareSum, 'joinSum' => $joinSum, 'returnSum' => $returnSum, 'activeSum' => $activeSum
        } else {
            return false;
        }
    }

    /**
     * 分红记录页
     */
    public function partnerRecord()
    {
        return view('Admin/partnerRecord');
    }

    /**
     * 分红记录
     */
    public function getPartnerRecord(Request $request)
    {
        $data = $request->all();
        $keyword = empty($data['keyword']) ? 0 : $data['keyword'];
        $rewardsData = PartnerRecord::orderBy('id', 'desc');
        //时间
        if (!empty($keyword['time'])) {
            $keyword['time'][0] = strtotime($keyword['time'][0]);
            $keyword['time'][1] = strtotime($keyword['time'][1]);
            $rewardsData = $rewardsData ->where('time', '>', $keyword['time'][0])->where('time', '<', $keyword['time'][1]);
        }
        $rewardsData = $rewardsData->paginate(15);
        foreach ($rewardsData as $v) {
            $v['time'] = date('Y-m-d H:i:s', $v['time']);
        }
        if ($rewardsData) {
            return ['code' => 1, 'data' => $rewardsData];
        } else {
            return ['code' => 0, 'data' => '没有数据'];
        }
    }
}
